package 力扣日常刷题.木23一月.第12天312;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author 帅小伙
 * @date 2022/3/12
 * @description
 */
public class Demo53合并区间 {

    public int[][] merge(int[][] intervals) {
        if(intervals.length == 0) return new int[0][2];
        Arrays.sort(intervals,(a,b)->Integer.compare(a[0],b[0]));
        ArrayList<int[]> ans = new ArrayList<>();

        for (int i = 0; i < intervals.length; i++) {
            int l = intervals[i][0],r = intervals[i][1];
            if(ans.size() == 0 || ans.get(ans.size() - 1)[1] < l) {
                ans.add(new int[]{l,r});
            }else{
                ans.get(ans.size() - 1)[1] = Math.max(ans.get(ans.size() - 1)[1],r);
            }
        }
        return ans.toArray(new int[ans.size()][]);
    }


}
